package com.symone.webcms.controller.web;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.*;
import com.symone.webcms.model.vo.ActionVO;
import com.symone.webcms.service.*;
import com.xiaoleilu.hutool.util.NumberUtil;
import io.swagger.annotations.ApiImplicitParam;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.List;

@Controller
public class DevController extends BaseController {

    @Autowired
    private ModuleService moduleService;
    @Autowired
    private ModuleFieldService moduleFieldService;
    @Autowired
    private ConfigService configService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private MenuActionService actionService;

    @RequestMapping(value = "/cms/dev/module/index", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:index")
    public ModelAndView moduleIndex() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/module/index");
        return mv;
    }

    @RequestMapping(value = "/cms/dev/module/add", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:add")
    public ModelAndView moduleAdd() {
        ModelAndView mv = new ModelAndView();
        mv.addObject("module", new Module());
        mv.setViewName("/dev/module/add");
        return mv;
    }

    @RequestMapping(value = "/cms/dev/module/edit", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:edit")
    @ApiImplicitParam(name="id",value="模型",required=true)
    public ModelAndView moduleEdit(Long id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("module", moduleService.selectById(id));
        mv.setViewName("/dev/module/edit");
        return mv;
    }

    @RequestMapping(value = "/cms/dev/module/get", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:index")
    @ApiImplicitParam(name="id",value="模型",required=true)
    public ModelAndView moduleView(Long id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("module", moduleService.selectById(id));
        mv.setViewName("/dev/module/info");
        return mv;
    }

    @RequestMapping(value = "/cms/dev/field/index", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:field:index")
    public ModelAndView fieldIndex() {
        List<Module> modules = moduleService.selectList(new EntityWrapper<Module>().eq("deleted",0));
        //Module module = moduleService.selectById(id);
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/field/index");
        //mv.addObject("module", module);
        mv.addObject("modules", modules);
        return mv;
    }

    @RequestMapping(value = "/cms/dev/field/add", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:field:add")
    @ApiImplicitParam(name="moduleId",value="模型ID",required=true)
    public ModelAndView fieldAdd(Long moduleId) {
        ModelAndView mv = new ModelAndView();
        if(moduleId != null && moduleId > 0 ){
            Module module = moduleService.selectById(moduleId);
            mv.addObject("field", new ModuleField());
            mv.addObject("module", module);
            mv.setViewName("/dev/field/add");
        }else {
            mv.setViewName("redirect:/error");
        }
        return mv;
    }

    @RequestMapping(value = "/cms/dev/field/edit", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:field:edit")
    @ApiImplicitParam(name="id",value="模型",required=true)
    public ModelAndView fieldEdit(Long id) {
        ModuleField field = moduleFieldService.selectById(id);
        Module module = moduleService.selectOne(new EntityWrapper<Module>().eq("code",field.getModule()));
        ModelAndView mv = new ModelAndView();
        mv.addObject("field", moduleFieldService.selectById(id));
        mv.addObject("module", module);
        mv.setViewName("/dev/field/edit");
        return mv;
    }


    @RequestMapping(value = "/cms/dev/field/get", method = RequestMethod.GET)
    @RequiresPermissions("dev:module:field:index")
    @ApiImplicitParam(name="id",value="模型",required=true)
    public ModelAndView fieldView(Long id) {
        ModelAndView mv = new ModelAndView();
        mv.addObject("field", moduleFieldService.selectById(id));
        mv.setViewName("/dev/field/info");
        return mv;
    }

    @RequiresPermissions("dev:config:index")
    @RequestMapping(value = "/cms/dev/config/index", method = RequestMethod.GET)
    public ModelAndView configList() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/config/index");
        return mv;
    }


    @RequiresPermissions("dev:config:add")
    @RequestMapping(value = "/cms/dev/config/add", method = RequestMethod.GET)
    public ModelAndView configAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/config/form");
        mv.addObject("config", new Config());
        return mv;
    }

    @RequiresPermissions("dev:config:edit")
    @RequestMapping(value = "/cms/dev/config/edit", method = RequestMethod.GET)
    public ModelAndView configEdit(Long id) {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/config/form");
        mv.addObject("config", configService.selectById(id));
        return mv;
    }


    @RequiresPermissions("dev:menu:index")
    @RequestMapping(value = "/cms/dev/menu/index", method = RequestMethod.GET)
    public ModelAndView menuIndex() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/menu/index");
        mv.addObject("menus",menuService.selectList(new EntityWrapper<Menu>().where("deleted=0")));
        return mv;
    }

    @RequiresPermissions("dev:menu:add")
    @RequestMapping(value = "/cms/dev/menu/add", method = RequestMethod.GET)
    public ModelAndView menuAdd() {
        List<Menu> menus = menuService.selectList(new EntityWrapper<Menu>().where("deleted=0 and pid=0"));

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/menu/form");
        mv.addObject("menu",new Menu());
        mv.addObject("menus",menus);
        mv.addObject("actions",getActionTemplate());
        return mv;
    }

    @RequiresPermissions("dev:menu:edit")
    @RequestMapping(value = "/cms/dev/menu/edit", method = RequestMethod.GET)
    public ModelAndView menuEdit(Long id) {
        Menu menu = new Menu().selectById(id);
        List<Menu> menus = menuService.selectList(new EntityWrapper<Menu>().where("deleted=0 and pid=0"));

        List<MenuAction> menuActions = actionService.selectList(new EntityWrapper<MenuAction>().eq("mcode",menu.getCode()));
        List<ActionVO> actionDTOList = getActionTemplate();
        List<ActionVO> actionList = new ArrayList<>();

        for(ActionVO att:actionDTOList){
            for(MenuAction action : menuActions){
                if(att.getCode().equals( action.getCode() )){
                    att.setChecked(true);
                }
            }
            actionList.add(att);
        }

        ModelAndView mv = new ModelAndView();
        mv.setViewName("/dev/menu/form");
        mv.addObject("menu",menu);
        mv.addObject("menus",menus);
        mv.addObject("actions",actionList);
        return mv;
    }

    private List<ActionVO> getActionTemplate(){
        List<ActionVO> actionDTOList = new ArrayList<>();

        ActionVO addAction = new ActionVO();
        addAction.setCode("add");
        addAction.setName("添加");
        addAction.setChecked(false);
        actionDTOList.add(addAction);

        ActionVO editAction = new ActionVO();
        editAction.setCode("edit");
        editAction.setName("编辑");
        editAction.setChecked(false);
        actionDTOList.add(editAction);

        ActionVO removeAction = new ActionVO();
        removeAction.setCode("remove");
        removeAction.setName("删除");
        removeAction.setChecked(false);
        actionDTOList.add(removeAction);

        return actionDTOList;
    }
}
